﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace DayDayUp.Core
{
    static class Utils
    {

   
        //public static Task<List<T>> ToListAsync<T>(this IQueryable<T> list)
        //{
        //    return Task.Run(() => list.ToList());
        //}

    }

    /// <summary>
    /// Paged list
    /// </summary>
    /// <typeparam name="T">T</typeparam>
    public class PagedListAsync<T> : List<T>, IPagedList<T> 
    {
        public IQueryable<T> Source{ get; set; }
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedListAsync(IQueryable<T> source, int pageIndex, int pageSize)
        {
            Source = source;
            int total = source.Count();
            this.TotalCount = total;
            this.TotalPages = total / pageSize;

            if (total % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            
        }

        public async Task<IPagedList<T>> Load()
        {
            var result = await Source.Skip(PageIndex * PageSize).Take(PageSize).ToListAsync();

            this.AddRange(result);
            return this;
        }

        ///// <summary>
        ///// Ctor
        ///// </summary>
        ///// <param name="source">source</param>
        ///// <param name="pageIndex">Page index</param>
        ///// <param name="pageSize">Page size</param>
        //public PagedListAsync(IList<T> source, int pageIndex, int pageSize)
        //{
        //    TotalCount = source.Count();
        //    TotalPages = TotalCount / pageSize;

        //    if (TotalCount % pageSize > 0)
        //        TotalPages++;

        //    this.PageSize = pageSize;
        //    this.PageIndex = pageIndex;
        //    this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        //}

        ///// <summary>
        ///// Ctor
        ///// </summary>
        ///// <param name="source">source</param>
        ///// <param name="pageIndex">Page index</param>
        ///// <param name="pageSize">Page size</param>
        ///// <param name="totalCount">Total count</param>
        //public PagedListAsync(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        //{
        //    TotalCount = totalCount;
        //    TotalPages = TotalCount / pageSize;

        //    if (TotalCount % pageSize > 0)
        //        TotalPages++;

        //    this.PageSize = pageSize;
        //    this.PageIndex = pageIndex;
        //    this.AddRange(source);
        //}

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        //public int TotalCountJD { get; set; }
        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPages); }
        }
    }
}
